package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.PositionDetailDao;
import com.JZ.QM.common.dao.UserPositionDetailDao;
import com.JZ.QM.common.dao.WealthPositionDao;
import com.JZ.QM.common.entity.PositionDetailEntity;
import com.JZ.QM.common.entity.UserPositionDetailEntity;
import com.JZ.QM.common.entity.WealthAccountEntity;
import com.JZ.QM.common.entity.WealthPositionEntity;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;
import com.JZ.QM.service.WealthPositionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: cyc
 * @email cyc@gmail.com
 * @date: 2020/7/12 22:55
 */
@Service()
public class WealthPositionServiceImpl extends ServiceImpl<WealthPositionDao, WealthPositionEntity> implements WealthPositionService {

    @Resource
    PositionDetailDao positionDetailDao;
    @Resource
    UserPositionDetailDao userPositionDetailDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<WealthPositionEntity> page = this.page(
                new Query<WealthPositionEntity>().getPage(params),
                new QueryWrapper<WealthPositionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取财富账号的id
     * @param accountId
     * @return
     */
    @Override
    public ArrayList<PositionDetailEntity> getWealthPositionProducts(Long accountId) {
        //持仓的详细信息信息列表
        ArrayList<PositionDetailEntity> positionDetailEntities=new ArrayList<>();
        //定义时间的格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");//设置日期格式
        //定义日历时间
        Calendar c = Calendar.getInstance();
        //前一天时间
        Date start=new Date();
        //前一天日期格式转换
        String combinationUpdateDate="";

        //取得前一天日期
        c.add(Calendar.DATE, -1);
        start = c.getTime();
        combinationUpdateDate = dateFormat.format(start);

        positionDetailEntities=positionDetailDao.getPositionDetailForOneDay(accountId,combinationUpdateDate);
        return positionDetailEntities;
    }

    /**
     * 通过组合id，获取所有购买该组合的金额最多的五名用户
     * @param comBaseId 原组合基本id
     * @return
     */
    @Override
    public ArrayList<UserPositionDetailEntity> showPositionTop5ForUser(Long comBaseId) {
        //持仓信息列表
        ArrayList<UserPositionDetailEntity> positionDetailEntities=new ArrayList<>();
        //用户持仓详细信息
        UserPositionDetailEntity userPositionDetailEntity=new UserPositionDetailEntity();
        //用户持仓详细收益
        UserPositionDetailEntity userPositionIncome=new UserPositionDetailEntity();
        //持仓总收益率
        BigDecimal positionRadio=new BigDecimal(0);

        //获取持有金额最多的五位用户
        positionDetailEntities=userPositionDetailDao.getPositionTop5ForUser(comBaseId);
        for (Iterator<UserPositionDetailEntity> it = positionDetailEntities.iterator(); it.hasNext(); ) {
            userPositionDetailEntity= it.next();
            //获取用户持仓的收益详细信息
            userPositionIncome=userPositionDetailDao.getUserPositionIncomeDetail(userPositionDetailEntity.getAccountId());
            //封装反悔的额结果集信息
            userPositionDetailEntity.setPositionDays(userPositionIncome.getPositionDays());
            positionRadio=userPositionIncome.getPositionRadio();
            //对为null的字段赋初始值
            if(positionRadio == null){
                positionRadio=new BigDecimal(0);
            }
            userPositionDetailEntity.setPositionRadio(positionRadio);
        }
        return positionDetailEntities;

    }

//    @Override
//    public ArrayList<PositionDetailEntity> getNewComPositionDetailItems(Long comId) {
//        //用户修改后的组合累积持仓信息
//        ArrayList<PositionDetailEntity> positionDetailEntities=new ArrayList<>();
//
//        positionDetailEntities=positionDetailDao.getNewComTotalPositionItems(comId);
//        return positionDetailEntities;
//    }
   
}
